package edu.rpi.tw.impav;

import java.awt.Color;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;


import processing.core.PGraphics;

import traer.physics.Spring;

/**
 * This class is a customized Java-fied version of the demo at:
 * http://www.cricketschirping.com/processing/GraphLayout2/
 * 
 * @author jim
 * 
 */
public class Edge extends Spring {
    String label;
    Color c = new Color(0);
    Graph g;
    Node a;
    Node b;

    private List<Tweet> tweets = new LinkedList<Tweet>();

    public void setLabel(String l) {
        label = l;
    }

    public void setColor(Color c) {
        this.c = c;
    }

    public Edge(Node a, Node b, Graph g) {
        super(a, b,
              Graph.SPRING_CONSTANT, Graph.DAMPING_CONSTANT,
                Graph.RESTLENGTH);
        this.a = a;
        this.b = b;
        this.g = g;
    }


    public void update(PGraphics graphics) {
        float weight = a.getTfIdf()*b.getTfIdf();
        float restLength = (float)Math.max((a.getRadius() + b.getRadius())*1.5f, 
                                             Graph.RESTLENGTH);
        setRestLength(restLength);
        //if (weight > 0)
        //    setStrength(Graph.SPRING_CONSTANT/ (weight*1000));
    }

    public void draw(PGraphics graphics) {
        if (!a.isVisible() || !b.isVisible()) return;
        long age = new Date().getTime()- this.tweets.get(tweets.size()-1).added.getTime();
        double fade = Math.pow(2, -(double)2*age/(double)g.lifetime);

        graphics.strokeWeight(2);
        double highlightFade = Math.pow(2, -(double)age/500f);
        graphics.stroke((float)(56f*highlightFade + 200), 
                (float)((150-150*highlightFade)+50), 
                (float)((200-200*highlightFade)+50), 
                (float)fade*256f);
        graphics.line((float)getOneEnd().position().x(), (float)
                getOneEnd().position().y(), (float) getTheOtherEnd()
                .position().x(), (float) getTheOtherEnd().position().y());
    }

    public List<Tweet> getTweets() {
        return tweets;
    }

    public void remove() {
        g.ps.removeSpring(this);
    }

}
